package com.ygxsk.carhome.ui.care;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.lxj.xpopup.core.AttachPopupView;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraKey;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.listener.DoSomeThingListener;
import com.ygxsk.carhome.ui.zxing.MyBGAQRCodeActivity;

/*
 * @Author:itchenqi175@163.com
 * @Time:2024/1/10  17:26
 * @Description:自定义的贴合弹窗：卡券发放的逻辑页面
 */
public class CustomAttachPopupBg extends AttachPopupView {

    //    private MaterialDialog materialDialogInPopu;

    private Context mContext;
    private Activity mActivity;

    private DoSomeThingListener doSomeThingListener;

    public CustomAttachPopupBg(@NonNull Context context, Activity activity, DoSomeThingListener _doSomeThingListener) {
        super(context);

        mContext = context;
        mActivity = activity;
        doSomeThingListener = _doSomeThingListener;

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.custom_attach_popupbg;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        //点击事件内容：
        //发送优惠券
        LinearLayout linearlayout_push = (LinearLayout) this.findViewById(R.id.linearlayout_push);
        //核销历史记录
//        LinearLayout linearlayout_dist = (LinearLayout) this.findViewById(R.id.linearlayout_dist);
        //执行扫码核销
        LinearLayout linearlayout_dist_qrcode = (LinearLayout) this.findViewById(R.id.linearlayout_dist_qrcode);

        linearlayout_push.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //携带信息跳转到【编辑-提交】页面；
                Bundle extra_bundle = new Bundle();
                //携带是 客户关怀 过去的 到卡券核销列表
                extra_bundle.putInt(ExtraKey.EXTRA_JUMPFROM_WHERE, ExtraValue.VALUE_JUMP_FORM_CARD_MAIN_ACTIVITY);
//              ActivityUtils.startActivity(extra_bundle, CardMainPushActivity.class);
                ActivityUtils.startActivity(extra_bundle, CardPushTabActivity.class);

                //弹窗关闭
                dismiss();

            }
        });


        linearlayout_dist_qrcode.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

//                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
//                    requestCodeQRCodePermissions();
//                } else {
//                    requestCodePermissionsXXPermissions();
//                }

//                openMain();

                if (doSomeThingListener != null) {
                    doSomeThingListener.doSomeThing();
                }

                dismiss();


            }
        });

    }

//    //android13的权限判断内容
//    private void requestCodePermissionsXXPermissions() {
//
//        try {
//            //do
//            if (!XXPermissions.isGranted(mActivity, com.hjq.permissions.Permission.CAMERA)//
//                    || !XXPermissions.isGranted(mActivity, com.hjq.permissions.Permission.READ_MEDIA_IMAGES)) {
//
//                CommUtils.checkMaterialDialog(materialDialogInPopu);
//
//                materialDialogInPopu = CommUtils.authorityRequest(mActivity, ConstantSign.MATERIALDIALOG_AUTHORITY_CONTENT_ALBUM, new AuthorityCallbackListener() {
//                    @Override
//                    public void doSomeThing() {
//
//                        CommUtils.checkMaterialDialog(materialDialogInPopu);
//
//                        XXPermissions.with(mActivity)
//                                // 适配分区存储应该这样写
//                                //.permission(Permission.Group.STORAGE)
//                                // 不适配分区存储应该这样写
//                                .permission(com.hjq.permissions.Permission.CAMERA)//
//                                .permission(com.hjq.permissions.Permission.READ_MEDIA_IMAGES)//
////                                    .permission(com.hjq.permissions.Permission.READ_MEDIA_VIDEO)//
////                                    .permission(com.hjq.permissions.Permission.READ_MEDIA_AUDIO)//
//                                //).permission(com.hjq.permissions.Permission.MANAGE_EXTERNAL_STORAGE
////                                                        .interceptor(new PermissionInterceptor())
//                                .request(new OnPermissionCallback() {
//
//                                    @Override
//                                    public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
//
//                                        if (!allGranted) {
//                                            // TODO: 2024/1/23 18:50 权限这里要不要给提示？权限没通过
//                                            return;
//                                        }
//
//                                        Toaster.show(String.format(mActivity.getResources().getString(R.string.demo_obtain_permission_success_hint), PermissionNameConvert.getPermissionString(mContext, permissions)));
//
//                                        openMain();
//
//                                    }
//                                });
//                    }
//                });
//
//            } else {
//
//                openMain();
//
//            }
//
//
//        } catch (Exception e) {
//            //catch
//            e.printStackTrace();
//        }
//
//
//    }
//
//    private void requestCodeQRCodePermissions() {
//
//        if (!AndPermission.hasPermissions(mActivity, Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)) {
//
//            CommUtils.checkMaterialDialog(materialDialogInPopu);
//
//            materialDialogInPopu = CommUtils.authorityRequest(mActivity, ConstantSign.MATERIALDIALOG_AUTHORITY_CONTENT_STORAGE, new AuthorityCallbackListener() {
//                @Override
//                public void doSomeThing() {
//
//                    CommUtils.checkMaterialDialog(materialDialogInPopu);
//
//                    //2018年06月05日发现这里添加SD卡的存储权限内容
//                    AndPermission.with(mActivity)//
//                            .runtime()//
//                            .permission(
//                                    Permission.CAMERA,//
//                                    Permission.READ_EXTERNAL_STORAGE,//
//                                    Permission.WRITE_EXTERNAL_STORAGE)//
//                            .rationale(new RuntimeRationale())//
//                            .onGranted(new Action<List<String>>() {//
//                                @Override
//                                public void onAction(List<String> permissions) {
//
//                                    openMain();
//
//                                }
//                            }).onDenied(new Action<List<String>>() {
//                                @Override
//                                public void onAction(List<String> permissions) {
//                                    //弹出失败弹窗
//                                    if (AndPermission.hasAlwaysDeniedPermission(mActivity, permissions)) {//
//                                        (new PermissionSetting(mContext)).showSetting(ConstantApi.PERMISSION_NORMAL, mActivity, permissions);//
//                                    }
//                                }
//                            }).start();
//
//                }
//            });
//
//        } else {
//
//            openMain();
//
//        }
//
//    }


    //打开扫描二维码的能力
    private void openMain() {

        Bundle bundle = new Bundle();
        bundle.putInt(ExtraKey.EXTRA_QRCODE_INFO, 0);
        bundle.putInt(ExtraKey.BGAQRCODE_KEY, ExtraValue.BGAQRCODE_VALUE_FROM_CARD_DIST_HISTORY);
        ActivityUtils.startActivity(bundle, MyBGAQRCodeActivity.class);

//        dismiss();

    }

    @Override
    protected void onShow() {
        super.onShow();

        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "onShow--onShow");

    }

    @Override
    protected void onDismiss() {
        super.onDismiss();

        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "onDismiss--onDismiss");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

//        CommUtils.checkMaterialDialog(materialDialogInPopu);

    }

}
